package jp.ac.aiit.spg.web.wbs;

import java.util.*;

import javax.ejb.EJB;

import org.seasar.teeda.extension.annotation.convert.DateTimeConverter;

import jp.ac.aiit.spg.AbstractSpgPage;
import jp.ac.aiit.spg.common.*;
import jp.ac.aiit.spg.entity.*;
import jp.ac.aiit.spg.service.SpgService;
import jp.ac.aiit.spg.web.login.LoginPage;

public class ListTasksPage extends AbstractSpgPage {

	@EJB
	private SpgService ss;

	private Collection<SpgTask> tasks;

	private List<SpgTaskItem> spgtaskDisplayItems = new ArrayList<SpgTaskItem>(); // この画面用の変数名を直感的にspgtaskItemsとしてはいけない。テーブル名とダブるから。

	private int taskId;

	private String wbsNumber;

	private String taskName;

	private int workingTime;

	@DateTimeConverter(pattern = "yyyy/MM/dd HH時")
	private Date startTimePlanned;

	@DateTimeConverter(pattern = "yyyy/MM/dd HH時")
	private Date finishTimePlanned;

	private String memberName;
	
	private String memberDescription;

	private String assignStatus;

	private String progressStatus;
	
	private int projectid;
	
	private String projectName;
	
	private String projectDescription;

	// ロジック
	public Class initialize() {

		// ログインチェック
		if (this.isLoggedIn() == false) {
			return LoginPage.class;
		} else {
			this.doListAllProjectTasks();
			return null;
		}


	}

	
	public Class prerender() {

		return null;
	}

	public Class doListAllProjectTasks() {

		setProjectInfo();

		spgtaskDisplayItems.clear();
		
		String[] sortOrder = {"wbsNumber"};
		tasks = ss.listAllProjectTask(projectid, sortOrder);
		
		setTasksInfo();
		
		return null;
	}
	
	public Class doListRejectedTasks() {

		setProjectInfo();

		spgtaskDisplayItems.clear();

		tasks = ss.listProjectTasksByAssignStatus(projectid, SpgAssignStatus.REJECTED);
		
		setTasksInfo();		
		
		return null;
		
	}



	public Class doListCreatedTasks() {
		
		setProjectInfo();

		spgtaskDisplayItems.clear();

		tasks = ss.listProjectTasksByAssignStatus(projectid, SpgAssignStatus.CREATED);
		setTasksInfo();		
		
		return null;
		
	}

	public Class doListAssignedTasks() {
		
		setProjectInfo();

		spgtaskDisplayItems.clear();

		tasks = ss.listProjectTasksByAssignStatus(projectid, SpgAssignStatus.ASSIGNED);
		setTasksInfo();		
		
		return null;
		
	}

	public Class doReCalcWbsNumber() {
		
		ss.reCalcWbsNumber(projectid);
		
		this.doListAllProjectTasks();
		return null;
		
	}
	

	private void setProjectInfo() {
		SpgProject p = ss.getProject(projectid);
		projectName = p.getName();
		projectDescription = p.getDescription();
	}

	private void setTasksInfo() {
		for (SpgTask t : tasks) {
			SpgTaskItem item = new SpgTaskItem();
			item.setTaskId(t.getId());
			item.setWbsNumber(t.getWbsNumber());
			item.setTaskName(t.getName());
			item.setWorkingTime(t.getWorkingTime());
			item.setStartTimePlanned(t.getStartTimePlanned());
			item.setFinishTimePlanned(t.getFinishTimePlanned());
			item.setMemberName(t.getMember().getUser().getName());
			item.setMemberDescription(t.getMember().getUser().getDescription());
			item.setAssignStatus(t.getAssignment().toString());
			item.setProgressStatus(t.getProgress().toString());
			spgtaskDisplayItems.add(item);
		}
	}


	

	// Setter/Getter
	public Date getStartTimePlanned() {
		return startTimePlanned;
	}

	public void setStartTimePlanned(Date startTimePlanned) {
		this.startTimePlanned = startTimePlanned;
	}

	public Date getFinishTimePlanned() {
		return finishTimePlanned;
	}

	public void setFinishTimePlanned(Date finishTimePlanned) {
		this.finishTimePlanned = finishTimePlanned;
	}

	public String getAssignStatus() {
		return assignStatus;
	}

	public void setAssignStatus(String status) {
		this.assignStatus = status;
	}

	public String getWbsNumber() {
		return wbsNumber;
	}

	public void setWbsNumber(String wbsNumber) {
		this.wbsNumber = wbsNumber;
	}

	public int getWorkingTime() {
		return workingTime;
	}

	public void setWorkingTime(int workingTime) {
		this.workingTime = workingTime;
	}

	public String getMemberName() {
		return memberName;
	}

	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}

	public List getSpgtaskDisplayItems() {
		return spgtaskDisplayItems;
	}

	public void setSpgtaskDisplayItems(List<SpgTaskItem> spgtaskDisplayItems) {
		this.spgtaskDisplayItems = spgtaskDisplayItems;
	}

	public int getTaskId() {
		return taskId;
	}

	public void setTaskId(int taskId) {
		this.taskId = taskId;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public int getProjectid() {
		return projectid;
	}

	public void setProjectid(int projectId) {
		this.projectid = projectId;
	}
	

	public String getProjectDescription() {
		return projectDescription;
	}

	public void setProjectDescription(String projectDescription) {
		this.projectDescription = projectDescription;
	}

	public String getProgressStatus() {
		return progressStatus;
	}

	public void setProgressStatus(String progressStatus) {
		this.progressStatus = progressStatus;
	}


	public String getMemberDescription() {
		return memberDescription;
	}


	public void setMemberDescription(String memberDescription) {
		this.memberDescription = memberDescription;
	}

}
